package com.yq.cp.controller.basics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.MyException;
import com.yq.cp.entity.basics.Position;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.User;
import com.yq.cp.entity.permission.vo.UserVo;
import com.yq.cp.entity.yw.YwZbgl;
import com.yq.cp.entity.yw.YwZbglGlry;
import com.yq.cp.entity.yw.YwZbglXq;
import com.yq.cp.service.basics.PositionService;
import com.yq.cp.service.permission.UserService;
import com.yq.cp.service.yw.YwZbglGlryService;
import com.yq.cp.service.yw.YwZbglService;
import com.yq.cp.service.yw.YwZbglXqService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 职位表 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/position")
public class PositionController {
    @Resource
    PositionService positionService;
    @Resource
    UserService userService;
    @Resource
    YwZbglService ywZbglService;
    @Resource
    YwZbglXqService ywZbglXqService;
    @Resource
    YwZbglGlryService ywZbglGlryService;
    /**
     * 添加编辑
     * @param position
     * @return
     */
    @PostMapping("addOrUpdate")
    public ApiResult addOrUpdate(@RequestBody Position position){
        positionService.add(position);
        return ApiResult.success();
    }

    /**
     * 删除
     * @param position
     * @return
     */
    @PostMapping("remove")
    public ApiResult remove(@RequestBody Position position){
        List<User> list = userService.list(new QueryWrapper<User>()
                .eq("position_id", position.getId())
                .eq("examine",1));
        if (!list.isEmpty())
            throw new MyException("该职位下有账号存在，请删除完后再试！！！");
        positionService.delete(position);
        return ApiResult.success();
    }

    /**
     * 职位管理列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        Page<Position> page =positionService.listToPosition(vo);
        return ApiResult.success(page);
    }

    /**
     * 组织结构添加编辑
     * @param vo
     * @return
     */
    @PostMapping("addStructure")
    public ApiResult addStructure(@RequestBody UserVo vo){
        userService.addStructure(vo);
        return ApiResult.success();
    }

    /**
     *组织架构列表
     * @param vo
     * @return
     */
    @PostMapping("structureList")
    public ApiResult structureList(@RequestBody PageVo vo){
        IPage<User> page =  userService.structureList(vo);
        return ApiResult.success(page);
    }

    /**
     * 删除绑定职位的用户
     * @param vo
     * @return
     */
    @PostMapping("removeTouser")
    public ApiResult removeTouser(@RequestBody User vo){
        User byId = userService.getById(vo);
        List<YwZbgl> nyz = ywZbglService.list(new QueryWrapper<YwZbgl>().eq("nyz_id", byId.getSiteId()));
        if (!nyz.isEmpty()){
            List<Long> collect = nyz.stream().map(YwZbgl::getId).collect(Collectors.toList());
            List<YwZbglXq> list = ywZbglXqService
                    .list(new QueryWrapper<YwZbglXq>().in("zbgl_id", collect));
            if (!list.isEmpty()){
                for (YwZbglXq ywZbglXq : list) {
                    List<YwZbglGlry> ywZbglGlrys = ywZbglGlryService.list(new QueryWrapper<YwZbglGlry>()
                            .eq("gl_id", ywZbglXq.getId())
                            .eq("user_id", byId.getId()));
                    if (!ywZbglGlrys.isEmpty())
                        throw new MyException("该人员为值班人员，请先删除值班人员后再试！！！");
                }
            }
        }
        userService.removeById(vo);
        return ApiResult.success();
    }

    /**
     * 根据名字和手机号匹配
     * @param vo
     * @return
     */
    @PostMapping("listUserToPosition")
    public ApiResult listUserToPosition(@RequestBody User vo){
        User user =  positionService.listUserToPosition(vo);
        return ApiResult.success(user);
    }

}

